﻿#region 版权声明

//

// 
// 代码开源主要用于解决大家在学习和研究协议过程中遇到由于缺乏代码所带来的制约性问题。
// 本代码仅供学习交流使用，大家在使用此开发包前请自行协调好多方面关系，
// 不得用于任何商业用途和非法用途，本人不享受和承担由此产生的任何权利以及任何法律责任。
// 
// 本源代码可通过以下网址获取:
// http://www.vckers.com
//
// Copyright @ 2009-2010  vckers.com.  All Rights Reserved.   
// Framework: 3.5
 
//   
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. 
  
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using TS.QQ;
using TS.QQ.Packets.In;
using TS.QQ.Packets.Out;
using TS.QQ.Packets;

using System.Configuration;

using System.Threading;
using System.Diagnostics;
using TS.QQ.Entities;
//add for interface
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Timers;
using TS.QQ.Utils;
using TS.QQ.Events;
//add for sign
using TS.QQ;
//using System.Windows.Forms
namespace ConsoleApplication1
{
    //屏蔽for interface
    //class Program
 internal class Program
    {
        /// <summary>
        /// 聊天的QQ好友
        /// </summary>
        //static int TQQ = 0;
        static TS.QQ.Entities.QQBasicInfo TQQ = null;
        //static bool AutoReply =false;
        static QQClient Client;
        //add for interface
        private static string adminqq = ConfigurationManager.AppSettings["AdminQQ"];
        private static string AutoReply = ConfigurationManager.AppSettings["AutoReply"];
        private static string back = ConfigurationManager.AppSettings["back"];
        private static string bianma = ConfigurationManager.AppSettings["encoding"];
        private static string getcode;
        private static string gettime = ConfigurationManager.AppSettings["gettime"];
        private static string qqnum = ConfigurationManager.AppSettings["qq"];
        private static string QQpassword = ConfigurationManager.AppSettings["password"];
        private static string Signature = ConfigurationManager.AppSettings["Signature"];
        private static string qunAutoReply = ConfigurationManager.AppSettings["qunAutoReply"];
        private static string returncode;
        private static string upcode = ConfigurationManager.AppSettings["upcode"];
        private static string url = ConfigurationManager.AppSettings["url"];
        private static string ys = ConfigurationManager.AppSettings["HIDDEN"];
        //add for interface over

        //static void Main(string[] args)
        //屏蔽原有main函数为调试接口
        //{
        //Init: Console.Title = "TS.NET QQRobots 2009 功能模板 ";
        //    StartLogin();
        //    AddEvents();
        //    HelpMessage();
        //    while (Client.LoginStatus==LoginStatus.Login)
        //    {
        //        string s=Console.ReadLine();
        //        if (s == "") continue;
        //        if (s.StartsWith("-") || s.StartsWith("/") || TQQ==null)
        //        {
        //            s=s.Replace("-","").Replace("/","");
        //            if (s.ToLower() == "x")
        //            {
        //                
        //                Client.LoginManager.Logout();
        //                Console.WriteLine();
        //                Console.WriteLine("退出成功");
        //                Client.LoginStatus = LoginStatus.Logout;
        //                break; 
        //            }
        //            ParseCommand(s);
        //        }
        //        else
        //        {
        //            if (TQQ != null)
        //            {

        //                    SendMsg(TQQ, s);
                        
                        
                        
        //            }
                    
        //        }
            
        //    }
        //    //Console.WriteLine();
        //    Echo("是否重新登录(Y/N):");
        //    if (Console.ReadLine().ToLower() == "y")
        //    {
        //        goto Init;
        //    }
            
        // }
        private static void Main(string[] args)
        {
            Console.Title = "QQRobots 2010 功能模板 ";
            do
            {
                Console.Title = "QQ机器人 For txj 状态：未登录";
                StartLogin();
                AddEvents();
                startmessage();
                HelpMessage();
                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Elapsed += new ElapsedEventHandler(Program.getsleep);
                double num = Convert.ToDouble(gettime);
                timer.Interval = num;
                timer.Enabled = true;
                while (Client.LoginStatus == LoginStatus.Login)
                {
                    string s = Console.ReadLine();
                    if ((s != "") && ((s.StartsWith("-") || s.StartsWith("/")) || (TQQ == null)))
                    {
                        s = s.Replace("-", "").Replace("/", "");
                        if (s.ToLower() == "x")
                        {
                            Client.LoginManager.Logout();
                            Console.WriteLine();
                            Console.WriteLine("退出成功");
                            Client.LoginStatus = LoginStatus.Logout;
                            break;
                        }
                        ParseCommand(s);
                    }
                    else
                    {//容错用于防止不可识别的字符串
                        Console.WriteLine("不可识别的命令！请键入help获取帮助信息");
                    }
                }
                Echo("是否重新登录(Y/N):");
            }
            while (Console.ReadLine().ToLower() == "y");
        }

        private static void AddEvents()
        {
            Client.PrivateManager.ChangeStatusSuccessed += new EventHandler<TS.QQ.Events.QQEventArgs<ChangeStatusReplyPacket, ChangeStatusPacket>>(PrivateManager_ChangeStatusSuccessed);
            Client.MessageManager.ReceiveKickOut += new EventHandler<TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket>>(MessageManager_ReceiveKickOut);
            //client2.PrivateManager.ChangeStatus(QQStatus.HIDDEN);
            Client.PrivateManager.GetLevelSuccessed += new EventHandler<TS.QQ.Events.QQEventArgs<GetLevelReplyPacket, GetLevelPacket>>(PrivateManager_GetLevelSuccessed);
            Client.PrivateManager.GetLevel();
            Client.FriendManager.DownloadGroupFriends(0);
            Client.FriendManager.GetFriendListSuccessed += new EventHandler<TS.QQ.Events.QQEventArgs<GetFriendListReplyPacket, GetFriendListPacket>>(FriendManager_GetFriendListSuccessed);
            Client.FriendManager.GetOnlineFriendSuccessed += new EventHandler<TS.QQ.Events.QQEventArgs<GetOnlineOpReplyPacket, GetOnlineOpPacket>>(FriendManager_GetOnlineFriendSuccessed);
            Client.FriendManager.GetFriendList();
            Client.MessageManager.ReceiveNormalIM += new EventHandler<TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket>>(MessageManager_ReceiveNormalIM);
            Client.FriendManager.FriendChangeStatus += new EventHandler<TS.QQ.Events.QQEventArgs<FriendChangeStatusPacket, OutPacket>>(FriendManager_FriendChangeStatus);
            Client.FriendManager.AddFriendNeedAuth += new EventHandler<TS.QQ.Events.QQEventArgs<AddFriendReplyPacket, AddFriendPacket>>(FriendManager_AddFriendNeedAuth);
            Client.FriendManager.AddFriendDeny += new EventHandler<TS.QQ.Events.QQEventArgs<AddFriendReplyPacket, AddFriendPacket>>(FriendManager_AddFriendDeny);
            Client.FriendManager.AddFriendNeedAnswer += new EventHandler<TS.QQ.Events.QQEventArgs<AddFriendReplyPacket, AddFriendPacket>>(FriendManager_AddFriendNeedAnswer);
            Client.FriendManager.AddFriendSuccessed += new EventHandler<TS.QQ.Events.QQEventArgs<AddFriendReplyPacket, AddFriendPacket>>(FriendManager_AddFriendSuccessed);
            Client.MessageManager.SysAddedByOthers += new EventHandler<TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket>>(MessageManager_SysAddedByOthers);
            Client.MessageManager.SysAddedByOthersEx += new EventHandler<TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket>>(MessageManager_SysAddedByOthersEx);
            Client.MessageManager.SysAddOtherApproved += new EventHandler<TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket>>(MessageManager_SysAddOtherApproved);
            Client.MessageManager.SysAddOtherRejected += new EventHandler<TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket>>(MessageManager_SysAddOtherRejected);
            Client.MessageManager.SysRequestAddMe += new EventHandler<TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket>>(MessageManager_SysRequestAddMe);
            Client.MessageManager.SysRequestAddMeEx += new EventHandler<TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket>>(MessageManager_SysRequestAddMeEx);
            Client.MessageManager.SysApprovedAddOtherAndAddMe += new EventHandler<TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket>>(MessageManager_SysApprovedAddOtherAndAddMe);
            Client.ClusterManager.ReceiveClusterIM += new EventHandler<TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket>>(ClusterManager_ReceiveClusterIM);
            Client.MessageManager.ReceiveVibration += new EventHandler<TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket>>(MessageManager_ReceiveVibration);
            Client.MessageManager.ReceiveInputState += new EventHandler<TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket>>(MessageManager_ReceiveInputState);
            //添加修改签名成功事件
            Client.PrivateManager.ModifySignatureSuccessed += new EventHandler<QQEventArgs<SignatureOpReplyPacket, SignatureOpPacket>>(FriendManager_ModifySignatureSuccessed);
            //Client.PrivateManager.ModifySignature(Signature);//登录时设置签名
            
            
        }

        static void MessageManager_ReceiveInputState(object sender, TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket> e)
        {
            Echo(string.Format("好友 {0}[{1}] 正在输入...", e.InPacket.Header.Sender, Client.QQUser.Friends[(int)e.InPacket.Header.Sender].Nick));
        }

        static void MessageManager_ReceiveVibration(object sender, TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket> e)
        {
            Echo(string.Format("收到好友 {0}[{1}] 振动", e.InPacket.Header.Sender, Client.QQUser.Friends[(int)e.InPacket.Header.Sender].Nick));
        }

        static void ClusterManager_ReceiveClusterIM(object sender, TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket> e)
        {
            if (back == "T")
            {
                Echo(string.Format("{0},{2}在群{1}中说:{3}", TS.QQ.Utils.Util.GetDateTimeFromMillis(e.InPacket.ClusterIM.SendTime), e.InPacket.Header.Sender, e.InPacket.ClusterIM.Sender, e.InPacket.ClusterIM.Message));
            }
            string lxid = "2";
            string qunid = e.InPacket.ClusterIM.ExternalId.ToString();
            string qunnid = e.InPacket.Header.Sender.ToString();
            string str4 = e.InPacket.ClusterIM.Sender.ToString();
            string message = e.InPacket.ClusterIM.Message;
            string nick = "";
            if (str4 != qqnum)
            {
                if (qunAutoReply == "update")
                {
                    if (str4 == adminqq)
                    {
                        web(lxid, qunid, qunnid, str4, nick, message);
                        if (returncode != "")
                        {
                            SendMsg(e.InPacket.NormalHeader.Sender, returncode);
                        }
                    }
                }
                else if (qunAutoReply == "start")
                {
                    web(lxid, qunid, qunnid, str4, nick, message);
                    if (returncode != "")
                    {
                        e.QQClient.ClusterManager.SendClusterIM(e.InPacket.Header.Sender, returncode);
                    }
                }
                else if (str4 == adminqq)
                {
                    web(lxid, qunid, qunnid, str4, nick, message);
                    if (returncode != "")
                    {
                        SendMsg(e.InPacket.NormalHeader.Sender, returncode);
                    }
                }
                else
                {
                    Echo("自动回复已关闭！您的消息无法回答！");
                }
            }
        }

        /// <summary>
        /// SendMsg
        /// </summary>
        private static void SendMsg(int qq, string msg)
        {
            if (Client.QQUser.Friends.ContainsKey(qq))
            {
                Client.MessageManager.SendIM(qq, msg);
                Echo(string.Format("你对 {0}[{1}] 说:{2}", qq, Client.QQUser.Friends[qq].Nick, msg));
            }
            else
            {
                Echo(" 该号码不存在，请首先使用-l命令查询好友或群信息");
            }
        }
        /// <summary>
        /// 重载SendMsg
        /// </summary>
        private static void SendMsg(QQBasicInfo TQQ, string msg)
        {

                switch (TQQ.Type)
                {
                    case QQType.QQ:
                        if (Client.QQUser.Friends.ContainsKey(TQQ.QQ))
                        {
                            Client.MessageManager.SendIM(TQQ.QQ, msg);
                            Echo(string.Format(DateTime.Now.ToShortTimeString() + ",你对 {0}[{1}] 说:\n\t{2}", TQQ.QQ, Client.QQUser.Friends[TQQ.QQ].Nick, msg));
                        }
                        else
                        {
                            Echo(" 该号码不存在，请首先使用-l命令查询好友信息");
                        }
                        break;
                    case QQType.Cluster:
                        if (Client.QQUser.ClusterList.ContainsKey(TQQ.QQ))
                        {
                            Client.ClusterManager.SendClusterIM(TQQ.QQ, msg);
                            Echo(string.Format(DateTime.Now.ToShortTimeString() + "你在群 {0}[{1}] 里说:\n\t{2}", Client.QQUser.ClusterList[TQQ.QQ].ExternalId, Client.QQUser.ClusterList[TQQ.QQ].Name, msg));
                        }
                        else
                        {
                            Echo(" 该群不存在，请首先使用-l命令查询群信息");
                        }
                        break;

                }

        }
        /// <summary>
        /// 此处需要再次重载SendMsg，用以适应多群消息相互转发，但需要防范多群互闪
        /// </summary>
        //private static void SendMsg(int HashSet QQList,string msg)
        //{ 
        
        //}
        private static void SendClusterMsg(int p, string msg)
        {
            
        }
        static void MessageManager_SysApprovedAddOtherAndAddMe(object sender, TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket> e)
        {
            Echo(string.Format("{0} 通过你的请求，并加你为好友", e.InPacket.From));
            //TS.QQ.Entities.QQFriend friend = new TS.QQ.Entities.QQFriend();
            //friend.QQBasicInfo.QQ = e.InPacket.From;
            e.QQClient.QQUser.Friends.Add(e.InPacket.From).FriendStatus.Status = QQStatus.我在线上;
        }

        static void FriendManager_AddFriendSuccessed(object sender, TS.QQ.Events.QQEventArgs<AddFriendReplyPacket, AddFriendPacket> e)
        {
            Echo(string.Format("添加好友 {0} 成功 ", e.InPacket.FriendQQ));
            e.QQClient.FriendManager.AddFriendToList(0, e.InPacket.FriendQQ);
        }

        static void FriendManager_AddFriendNeedAnswer(object sender, TS.QQ.Events.QQEventArgs<AddFriendReplyPacket, AddFriendPacket> e)
        {
            Echo(string.Format("添加QQ:{0} 需要回答问题 \n系统暂不支持回答问题的方式加好友", e.InPacket.FriendQQ)); 
        }

        static void FriendManager_AddFriendDeny(object sender, TS.QQ.Events.QQEventArgs<AddFriendReplyPacket, AddFriendPacket> e)
        {
            Echo(string.Format("QQ:{0} 拒绝加为好友", e.InPacket.FriendQQ)); 
        }

        static void FriendManager_AddFriendNeedAuth(object sender, TS.QQ.Events.QQEventArgs<AddFriendReplyPacket, AddFriendPacket> e)
        {
            Echo(string.Format("添加QQ:{0} 需要输入验证信息 \n请使用 -add {0} [验证信息] 的命令", e.InPacket.FriendQQ)); 
        }

        static void MessageManager_SysRequestAddMeEx(object sender, TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket> e)
        {
            if (back == "T")
            {
            Echo(string.Format("{0} 加你为好友,附加消息:{1}\n系统已经自动同意加对方为好友", e.InPacket.From, e.InPacket.Message));//输入 -add {0} 命令加{0}为好友
}
            //e.QQClient.FriendManager.ApprovedAddMe(e.InPacket.From);
            if (e.InPacket.ReverseAdd == RevenseAdd.Allow)//对方允许添加好友
            {
                e.QQClient.FriendManager.ApprovedAndAdd(e.InPacket.From);
                e.QQClient.FriendManager.AddFriendToList(0, e.InPacket.From);
                //TS.QQ.Entities.QQFriend friend=new TS.QQ.Entities.QQFriend();
                //friend.QQBasicInfo=e.InPacket.From;
                e.QQClient.QQUser.Friends.Add(e.InPacket.From).FriendStatus.Status = QQStatus.我在线上;
                string lxid = "1";
                string qunid = "0";
                string qunnid = "0";
                string nick = e.QQClient.QQUser.Friends[e.InPacket.From].Nick;
                string str5 = e.InPacket.From.ToString();
                string message = e.InPacket.Message;
                web(lxid, qunid, qunnid, str5, nick, message);
                SendMsg(e.InPacket.From, returncode);
            }
            else //走其它添加好友的方式
            {
                e.QQClient.FriendManager.ApprovedAdd(e.InPacket.From);
                //e.QQClient.FriendManager.AddFriendToList(0, e.InPacket.From);
                //e.QQClient.FriendManager.AddFriend(e.InPacket.From);
            }
        }

        static void MessageManager_SysRequestAddMe(object sender, TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket> e)
        {
            Echo(string.Format("{0} 加你为好友,附加消息:{1}\n输入 -add {0} 命令加{0}为好友", e.InPacket.From, e.InPacket.Message));
        }

        static void MessageManager_SysAddOtherRejected(object sender, TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket> e)
        {
            Echo(string.Format("{0} 拒绝加你为好友,附加消息:{1}", e.InPacket.From,e.InPacket.Message));
        }

        static void MessageManager_SysAddOtherApproved(object sender, TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket> e)
        {
            Echo(string.Format("{0} 通过您的认证", e.InPacket.From));
        }

        static void MessageManager_SysAddedByOthersEx(object sender, TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket> e)
        {
            Echo(string.Format("{0} 加你为好友,附加消息:{1}\n 输入 -add {0} 命令加{0}为好友", e.InPacket.From, e.InPacket.Message));
        }

        static void MessageManager_SysAddedByOthers(object sender, TS.QQ.Events.QQEventArgs<SystemNotificationPacket, OutPacket> e)
        {
            Echo(string.Format("{0} 加你为好友.",e.InPacket.From));
        }
     //屏蔽 StartLogin for interface
     //   private static void StartLogin()
     //   {
     //   Init:
//#if !RELEASE
    //        int qq=0;
    //        bool IsQQNum = false;
            //增加通过配置文件读取qq账号密码信息
            //增加通过命令行增加账号密码信息
    //        while (!IsQQNum)
    //        {
    //            try
    //            {
    //                Console.Write("QQ账号:");
                    //string s = Console.ReadLine();//先屏蔽手工输入便于调试
    //                string s= "20523167";
    //                Console.WriteLine("您输入的QQ号是:{0}", s);
    //                qq = int.Parse(s);
    //                IsQQNum = true;
    //            }
    //            catch
    //            {
    //                Console.WriteLine("QQ号码错误,请重新输入:");
    //                IsQQNum = false;
    //            }
    //        }
    //        Console.Write("密码:");
    //        string p = "";
            //p = Console.ReadLine();//先屏蔽手工输入便于调试
    //        p = "5551191844";
    //        Console.Write("是否隐身登陆(Y/N):");
    //        //string st = Console.ReadLine();//先屏蔽手工输入便于调试
    //        string st = "N";
    //        Console.Clear();
    //        QQUser user2 = new QQUser(qq, p);
    //        if(st=="Y"||st=="y")
    //        {
    //            user2.LoginMode = QQStatus.HIDDEN;
    //        }
//#else

//#endif
    //        Client = new QQClient(user2);
    //        Client.LoginManager.LoginStatusChanged += new EventHandler<TS.QQ.Events.LoginStatusChangedEventArgs<LoginStatus>>(LoginManager_LoginStatusChanged);
            //user2.IsUdp = true;//设置登录模式 udp 或tcp 
    //        Client.LoginServerHost = "219.133.49.171"; //指定默认的服务器IP地址，可以不指定服务器IP，系统自动搜索可用的IP
    //        Client.Login();
    //        while (Client.LoginStatus != LoginStatus.Login)//如果没有登录成功则
    //        {
    //            if (Client.LoginStatus == LoginStatus.NeedVerifyCode)
    //            {
    //                Console.Write("请输入验证码：");
    //                string code = Console.ReadLine();
    //                Client.LoginStatus = LoginStatus.SendVerifyCode;
    //                Client.LoginManager.LoginSendVerifyCode(code);

    //            }
    //            else if (Client.LoginStatus == LoginStatus.WrongPassword)
    //            {
    //                goto Init;
    //            }
    //            else if(Client.LoginStatus ==LoginStatus.ChangeServer)
    //            {
    //                Console.WriteLine("尝试连接："+Client.LoginServerHost);
    //            }
    //            Thread.Sleep(100);
    //        }
    //    }
        /// <summary>
        /// 开始登陆
        /// </summary>
        private static void StartLogin()
        {
            int num;
        Label_0000:
            num = 0;
            bool flag = false;
            while (!flag)
            {
                try
                {
                    Console.Write("QQ账号:" + Program.qqnum + "\n");
                    string qqnum = Program.qqnum;
                    num = int.Parse(qqnum);
                    flag = true;
                    Console.Title = "QQ机器人 For txj 当前QQ：" + qqnum;
                    continue;
                }
                catch
                {
                    Console.WriteLine("QQ号码错误,请关闭机器人重新配置！");
                    flag = false;
                    Console.ReadLine();
                    continue;
                }
            }
            Console.Write("密码:************\n");
            string qQpassword = QQpassword;
            Console.Write("是否隐身登陆(Y/N):" + Program.ys + "\n");
            string ys = Program.ys;
            QQUser user = new QQUser(num, qQpassword);
            switch (ys)
            {
                case "Y":
                case "y":
                    user.LoginMode = QQStatus.HIDDEN;
                    break;
            }
            Client = new QQClient(user);
            Client.LoginManager.LoginStatusChanged += new EventHandler<LoginStatusChangedEventArgs<LoginStatus>>(Program.LoginManager_LoginStatusChanged);
            Client.LoginServerHost = "219.133.49.171";
            Client.Login();
            while (Client.LoginStatus != LoginStatus.Login)
            {
                if (Client.LoginStatus == LoginStatus.NeedVerifyCode)
                {
                    Console.Write("请输入验证码：");
                    string verifyCode = Console.ReadLine();
                    Client.LoginStatus = LoginStatus.SendVerifyCode;
                    Client.LoginManager.LoginSendVerifyCode(verifyCode);
                }
                else if (Client.LoginStatus == LoginStatus.WrongPassword)
                {
                    goto Label_0000;
                }
                Thread.Sleep(100);
            }
            Console.Clear();
        }
        static void MessageManager_ReceiveKickOut(object sender, TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket> e)
        {
            Client.LoginStatus = LoginStatus.Logout;
            Echo(e.InPacket.SysMessage);
            
        }

        static void FriendManager_FriendChangeStatus(object sender, TS.QQ.Events.QQEventArgs<FriendChangeStatusPacket, OutPacket> e)
        {
            Echo(string.Format("好友{0}[{1}],更改状态为 {2}", e.InPacket.FriendQQ.ToString(),e.QQClient.QQUser.Friends[(int)e.InPacket.FriendQQ].Nick,  e.InPacket.Status.ToString()));
        }

        private static void ParseCommand(string s)
        {
            string[] args = s.Split(new char[] { ' ', '\t' },StringSplitOptions.RemoveEmptyEntries);
            if (args.Length > 0)
            {
                switch (args[0].ToLower())
                { 
                    case "t":
                    case "to":
                        if (args.Length > 2)
                            {
                                int qqnum =0;
                                int.TryParse(args[1], out qqnum);
                                //if (Client.QQUser.Friends.ContainsKey(qqnum))
                               // {
                                    SendMsg(qqnum, args[2]);
                                    //Echo("two parm");
                               // }
                               // else if (Client.QQUser.ClusterList.ContainsKey(qqnum))
                               // {
                               //     SendMsg(qqnum, args[2]);
                               // }
                               // else
                               // {
                               //     Echo("未找到该QQ好友！请使用?获取帮助");
                               // }
                            }//此处需要增加直接发送消息的方法
                            
                        else 
                            ChatToQQ(args[1]);
                        break;
                    case "tq":
                    case "toqun":
                        //if (args.Length > 2)
                        //{
                        //    int qqqnum = 0;
                        //    int.TryParse(args[1], out qqqnum);
                            //if (Client.QQUser.ClusterList.ContainsKey(qqqnum))
                           // {
                           //     SendMsg(qqqnum, args[2]);
                                //Echo("two parm");
                            //}
                           // else
                           //     {
                           //         Echo("未找到该QQ群！"); 
                           //     }
                        // }
                        //else
                        //    ChatToQQ(args[1]);
                        try
                        {
                            int.Parse(args[1]);
                        }
                        catch (Exception)
                        {
                            Echo("发送消息失败，请输入正确的群号码！");
                            return;
                        }
                        if (args.Length > 1)
                        {
                            if (args.Length <= 2)
                            {
                                Echo("程序指令出错！请检查！");
                                return;
                            }
                            int num3 = 0;
                            if (!int.TryParse(args[1], out num3))
                            {
                                return;
                            }
                            if (!Client.QQUser.ClusterList.ContainsKey(num3))
                            {
                                Echo("未找到该群号码！");
                                return;
                            }
                            int num4 = 2;
                            string str2 = "";
                            while (num4 < args.Length)
                            {
                                str2 = str2 + args[num4] + " ";
                                num4++;
                            }
                            TQQ = Client.QQUser.ClusterList[num3].QQBasicInfo;
                            SendMsg(TQQ, str2);
                            TQQ = null;
                        }
                        
                       break;
                    case "l":
                    case "list":
                    case "view":
                        List();
                        break;
                    case "o":
                    case "ol":
                    case "onlines":
                    case "listonlines":
                        OnLineList();
                        break;
                    case "add":
                    case "+":
                    case "a"://应该不要吧
                        int q = 0;
                        try
                        {
                            q=int.Parse(args[1]);
                        }
                        catch(Exception e)
                        {
                            Echo("添加失败，请输入正确的QQ号码！"+e);
                            break;
                        }
                        if (args.Length > 2)
                        {
                            Client.FriendManager.SendAddFriendAuth(q, args[2]);
                            Echo("添加好友:" + q.ToString() + ",附加消息" + args[2]);
                        }
                        else
                        {
                            Client.FriendManager.AddFriend(q);
                            Echo("添加好友:" + q.ToString() );
                        }
                        FriendAction();
                        break;
                    case "sign":

                        if (args.Length < 2 || args.Length>2)
                        {
                            
                            Echo("参数错误");
                        }
                        else
                         {

                             Echo(args[1]);
                            if (args[1]=="''")
                                //
                            {//删除签名
                                Client.PrivateManager.DeleteSignature();
                            }
                            else
                            {//修改签名

                                Client.PrivateManager.ModifySignature(utf2gbk(args[1]));
                                //if (PM.ModifySignatureSuccessed == true)
                                //    Echo("success");
                            }
                        }
                        break;
                    case "v":
                    case "vibration":
                        if (args.Length > 1)
                        {
                            int qqnum = 0;
                            if (int.TryParse(args[1], out qqnum))
                            {
                                if (Client.QQUser.Friends.ContainsKey(qqnum))
                                {
                                    int t = 1;
                                    if (args.Length > 2)
                                        int.TryParse(args[2], out t);
                                    for (int i = 0; i < t; i++)
                                    {
                                        Client.MessageManager.SendVibration(qqnum);
                                        Echo(string.Format("你给 {1}[{0}] 发送振动", qqnum, Client.QQUser.Friends[qqnum].Nick));
                                        Thread.Sleep(10000);//休眠 防止发送过快
                                    }


                                }
                                else
                                    Echo("未找到该QQ好友！");
                            }
                            else 
                            {
                                Echo("QQ号错误，命令格式为: -v 123456");
                            }
                        }
                        else
                        {
                            if (TQQ != null)
                            {
                                Client.MessageManager.SendVibration(TQQ.QQ);
                                Echo(string.Format("你给 {1}[{0}] 发送振动", TQQ, Client.QQUser.Friends[TQQ.QQ].Nick));
                            }
                        }
                        break;

                    case "h":
                    case "help":
                    case "?":
                        HelpMessage();
                        break;
                    case "c":
                    case "cls":
                        Console.Clear();
                        break;

                }
            
            }
        }

        /// <summary>
        /// 和谁聊天
        /// </summary>
        /// <param name="args"></param>
        private static void ChatToQQ(string To)
        {
            int qqnum = 0;
            if (int.TryParse(To, out qqnum))
            {
                if (Client.QQUser.Friends.ContainsKey(qqnum))
                {
                    TQQ = Client.QQUser.QQList[qqnum];
                    Echo("你将和 " + TQQ.QQ.ToString() + "[" + Client.QQUser.Friends[TQQ.QQ].Nick + "] 聊天。");


                }
                else if (Client.QQUser.ClusterList.ContainsKey(qqnum))
                {
                    TQQ = Client.QQUser.ClusterList[qqnum].QQBasicInfo;
                    Echo("你将在群 " + TQQ.QQ.ToString() + "[" + Client.QQUser.ClusterList[TQQ.QQ].Name + "] 中聊天。");
                }
                else
                {
                    Echo("输入的QQ号错误，当前好友列表中没有该好友！请使用/view命令查看好友列表");
                }

            }
            else
            {
                //var q=from fi in Client.QQUser.Friends.Values where  fi.
                List<QQFriend> fl = new List<QQFriend>();
                foreach (QQFriend  Friend in Client.QQUser.Friends.Values)
                {
                    if (Friend.Nick != null && Friend.Nick.Contains(To))
                    {
                        fl.Add(Friend);
                    }
                }
                if (fl.Count == 0)
                {
                    Echo("好友列表中没有该好友！请使用/view命令查看好友列表");
                }
                else if (fl.Count == 1)
                {
                    TQQ = fl[0].QQBasicInfo;
                    Echo("你将和 " + TQQ.QQ.ToString() + "[" + Client.QQUser.Friends[TQQ.QQ].Nick + "] 聊天。");
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("共找到 " + fl.Count.ToString() + " 位好友,请使用-To QQ号命令从下面选择一位好友");
                    sb.AppendLine(string.Format("{0,-12}{1,-20}\t{2,-5}\t{3,-5}\t{4,-5}\t{5,5}", "好友", "昵称", "头像", "年龄", "性别", "状态"));
                    foreach (QQFriend Friend in fl)
                    {
                        sb.AppendLine(string.Format("{0,-12}{1,-20}\t{2,7}\t{3,7}\t{4,7}\t{5,7}", Friend.QQ, Friend.Nick, Friend.Header.ToString(), Friend.Age, Friend.Gender, Friend.FriendStatus.Status.ToString()));
                    }
                    Echo(sb.ToString());
                }
            }
        }
        //屏蔽for interface
        //static void MessageManager_ReceiveNormalIM(object sender, TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket> e)
        //{
        //    Echo(string.Format("{0},{1}[{2}] 说:{3}",TS.QQ.Utils.Util.GetDateTimeFromMillis( e.InPacket.NormalHeader.SendTime), e.InPacket.Header.Sender,e.QQClient.QQUser.Friends[(int)e.InPacket.Header.Sender].Nick, e.InPacket.NormalIM.Message));
        //}


        static void FriendManager_GetOnlineFriendSuccessed(object sender, TS.QQ.Events.QQEventArgs<GetOnlineOpReplyPacket, GetOnlineOpPacket> e)
        {
            Console.WriteLine("获取在线好友列表成功！");
            
        }

        static void FriendManager_GetFriendListSuccessed(object sender, TS.QQ.Events.QQEventArgs<GetFriendListReplyPacket, GetFriendListPacket> e)
        {
           // Console.WriteLine("获取好友列表成功！");
          //Console.WriteLine("好友\t\t\t昵称\t头像\tAge\tsex");
           // foreach (KeyValuePair<int, TS.QQ.FriendList.FriendInfo> Item in e.QQClient.QQUser.Friends)
           // {
           //     Console.WriteLine(string.Format("{0,-12}\t{1,-15}\t{2}\t{3}\t{4}", Item.Value.BasicInfo.QQ, Item.Value.BasicInfo.Nick, Item.Value.BasicInfo.Header.ToString(), Item.Value.BasicInfo.Age, Item.Value.BasicInfo.Gender));
           // }
            e.QQClient.FriendManager.GetOnlineFriend();
        }
     //修改签名成功
        static void FriendManager_ModifySignatureSuccessed(object sender,   TS.QQ.Events.QQEventArgs<SignatureOpReplyPacket, SignatureOpPacket> e)
        {
            Echo("签名修改成功！");
        }
        static void PrivateManager_GetLevelSuccessed(object sender, TS.QQ.Events.QQEventArgs<GetLevelReplyPacket, GetLevelPacket> e)
        {
            Echo(string.Format("Level:{0}  ActiveDays:{1} UpgradeDays:{2}", e.InPacket.Level, e.InPacket.ActiveDays, e.InPacket.UpgradeDays));
            
        }

        static void PrivateManager_ChangeStatusSuccessed(object sender, TS.QQ.Events.QQEventArgs<ChangeStatusReplyPacket, ChangeStatusPacket> e)
        {
            Echo("改变状态成功：" + e.QQClient.QQUser.Status.ToString());
        }

        static void LoginManager_LoginStatusChanged(object sender, TS.QQ.Events.LoginStatusChangedEventArgs<LoginStatus> e)
        {
            if (e.LoginStatus == LoginStatus.ChangeServer)
            {
                Echo(e.LoginStatus.ToString()+":"+Client.LoginServerHost);
            }else
            Echo(e.LoginStatus.ToString());
        }

        static void LoginManager_LoginNeedVerifyCode(object sender, TS.QQ.Events.QQEventArgs<TS.QQ.Packets.In.LoginRequestReplyPacket, TS.QQ.Packets.Out.LoginRequestPacket> e)
        {
            Console.Write("请输入验证码("+e.InPacket.CodeFileName+")：");
            string code=Console.ReadLine();
            e.QQClient.LoginManager.LoginSendVerifyCode(code);
        }

        static string utf2gb2312(string str)
        {
            byte[] bytes = System.Text.Encoding.GetEncoding("Unicode").GetBytes(str);
            str = System.Text.Encoding.GetEncoding("gb2312").GetString(bytes);
            return str;
        }
        static string gb23122utf(string str)
        {
            byte[] bytes = System.Text.Encoding.GetEncoding("gb2312").GetBytes(str);
            str = System.Text.Encoding.GetEncoding("Unicode").GetString(bytes);
            return str;
        }
        static string utf2gbk(string str)
        { 
            byte[] bytes = System.Text.Encoding.GetEncoding("Unicode").GetBytes(str);
            str = System.Text.Encoding.GetEncoding("gbk").GetString(bytes);
            return str;
        }
        static string gbk2utf(string str)
        {
            byte[] bytes = System.Text.Encoding.GetEncoding("gbk").GetBytes(str);
            str = System.Text.Encoding.GetEncoding("Unicode").GetString(bytes);
            return str;
        }
        static void HelpMessage()
        {
            StringBuilder sb = new StringBuilder();
            //TS.QQ.Net.WebClient client = new TS.QQ.Net.WebClient();
            //client.Encoding = System.Text.Encoding.Default;//默认编码方式，根据需要设置其他类型
            //client.OpenRead("http://www.baidu.com");//普通get请求
            //sb.AppendLine(client.RespHtml);
            sb.AppendLine("==========================================================");
            sb.AppendLine("本软件基于QQ2009协议开发，仅用于学习研究，不得用于商业用途");
            sb.AppendLine("代码开发过程参阅了LumaQQ.net(C#.net)、MYQQ(C++)等源代码");
            sb.AppendLine("在这里对LumaQQ.net(C#.net)、MYQQ(C++)作品的开发人员表示感谢！");
            sb.AppendLine("==========================================================");
            sb.AppendLine("帮助信息:");
            sb.AppendLine("\t-h help ? 显示帮助信息;");
            sb.AppendLine("\t-t to 与好友对话;");
            sb.AppendLine("\t\t-t to 123456 与QQ号为123456的好友进入对话模式;");
            sb.AppendLine("\t\t-t to 张三 与昵称为张三的好友进入对话模式;");
            sb.AppendLine("\t\t-t tq 123456 与QQ群号为123456的群进入对话模式;");
            sb.AppendLine("\t\t-t toqun 123456 与QQ群号为123456的好友进入对话模式;");
            //
            sb.AppendLine("\t\t-t to 123456 早上好! 与QQ号为123456的好友说“早上好！”;");
            sb.AppendLine("\t\t-t to 张三 早上好！ 与昵称为张三的好友;");
            sb.AppendLine("\t\t-t tq 123456 早上好！ 在QQ群123456中说“早上好”;");
            sb.AppendLine("\t\t-t toqun 123456 早上好！ 在QQ群123456中说“早上好！”;");
            //
            sb.AppendLine("\t-a add 添加好友;");
            sb.AppendLine("\t\t-add 12345 添加12345为好友;");
            sb.AppendLine("\t-l list view 显示全部好友信息;");
            sb.AppendLine("\t-0 lo onlines listOnlines 显示全部在线好友信息;");
            sb.AppendLine("\t-v vibration 给好友发送振动;");
            sb.AppendLine("\t\t-v 给当前聊天好友发送振动;");
            //临时屏蔽签名功能，待解决编码问题
            //sb.AppendLine("\t-sign 签名内容 给机器人设置签名;");
            sb.AppendLine("\t-v 123456 10  给好友123456发送10次振动;");
            sb.AppendLine("\t-c cls 清屏幕;");
            sb.AppendLine("\t-x exit 退出。");
            Echo(sb.ToString());
        }
        static void Sign()
        {
            ////
            //sign.
        }
        static void List()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("QQ好友:{0}",Client.QQUser.Friends.Count));
            sb.AppendLine(string.Format("{0,-12}{1,-18}\t{2,-5}\t{3,-5}\t{4,-5}\t{5,5}", "好友", "昵称", "头像", "年龄", "性别", "状态"));
            foreach (QQFriend Friend in Client.QQUser.Friends.Values)
            {
                sb.AppendLine(string.Format("{0,-12}{1,-16}\t{2,7}\t{3,7}\t{4,7}\t{5,7}", Friend.QQBasicInfo.QQ, Friend.Nick, Friend.Header.ToString(), Friend.Age, Friend.Gender, Friend.Status.ToString()));
                //sb.AppendLine(string.Format("{6,2}",TS.QQ.FriendManager.GetSignature(Friend.QQ));
            }
            sb.AppendLine(string.Format("QQ群:{0} 个", Client.QQUser.ClusterList.Count));
            sb.AppendLine(string.Format("{0}\t\t{1}\t{2}", "UIN","群号","群名"));
            foreach(TS.QQ.Entities.ClusterInfo clusterInfo in Client.QQUser.ClusterList.Values)
            {
                sb.AppendLine(string.Format("{0}\t{1}\t{2}", clusterInfo.QQBasicInfo.QQ, clusterInfo.ExternalId, clusterInfo.Name));
                //sb.AppendLine(string.Format("{0}\t{1}\t{2}", clusterInfo.AuthType, clusterInfo.Category, clusterInfo.ClusterId));
                //sb.AppendLine(string.Format("{0}\t{1}\t{2}", clusterInfo.Creator, clusterInfo.Description, clusterInfo.ExternalId));
                //sb.AppendLine(string.Format("{0}\t{1}\t{2}", clusterInfo.Name, clusterInfo.Notice, clusterInfo.OldCategory));
                //sb.AppendLine(string.Format("{0}\t{1}\t{2}", clusterInfo.QQBasicInfo, clusterInfo.Type, clusterInfo.Unknown1));
                //sb.AppendLine(string.Format("{0}\t{1}\t{2}", clusterInfo.Unknown2, clusterInfo.Unknown3, clusterInfo.Unknown4));
                //sb.AppendLine(string.Format("{0}\t{1}", clusterInfo.Unknown5, clusterInfo.VersionId));
            }
            Echo(sb.ToString());
        }

        static void OnLineList()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("{0,-12}{1,-18}\t{2,-5}\t{3,-5}\t{4,-5}\t{5,5}", "好友", "昵称", "头像", "年龄", "性别", "状态"));
            foreach (QQFriend Friend in Client.QQUser.Friends.Values)
            {
                if (Friend.FriendStatus.Status != QQStatus.NONE && Friend.FriendStatus.Status != QQStatus.离线或隐身)
                {
                    sb.AppendLine(string.Format("{0,-12}{1,-16}\t{2,7}\t{3,7}\t{4,7}\t{5,7}", Friend.QQBasicInfo.QQ, Friend.Nick, Friend.Header.ToString(), Friend.Age, Friend.Gender, Friend.Status.ToString()));
                }
            }
            Echo(sb.ToString());
        }
        static void Echo(string s)
        {
            if (Client.LoginStatus == LoginStatus.Login)
            {

                Console.WriteLine();
                //Console.Write(s);
                s = s.Replace("\r\n", "\n").Replace("\r","\n");
                string[] ss = s.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s1 in ss)
                {
                    Console.WriteLine(s1);
                }
                if (TQQ != null)
                {
                    if (TQQ.Type == QQType.QQ)
                    {
                        Console.Write("\nQQ>好友:" + TQQ.QQ.ToString() + "[" + Client.QQUser.Friends[TQQ.QQ].Nick + "]>");
                    }else
                        Console.Write("\nQQ>群:" + TQQ.QQ.ToString() + "[" + Client.QQUser.ClusterList[TQQ.QQ].Name + "]>");
                }
                else
                {
                    Console.Write("\nQQ>");
                }
            }else
                Console.WriteLine(s);
            //if(Console.
        }

        static void FriendAction()
        {

            do
            {
                List<int> RemoveItems =new List<int>();
                foreach (FriendActionInfo FAI in Client.FriendManager.FriendActionList.Values)
                {
                    if (FAI.ActionProgress == FriendActionProgress.NeedVerify)
                    {
                        Console.WriteLine();
                        Console.Write(string.Format("验证码保存在：{0}\n 请输入验证码：", FAI.Message));
                        string code = Console.ReadLine();
                        FAI.VCode = code;
                    }
                    else if (FAI.ActionProgress == FriendActionProgress.NeedAuthor)
                    {
                        Console.Write(string.Format("添加好友 {0} 需要验证信息：", FAI.QQ));
                        FAI.AuthorMessage = Console.ReadLine();
                    }
                    else if (FAI.ActionProgress == FriendActionProgress.NeedAnswer)
                    {
                        Console.Write(string.Format("添加好友 {0} 请回答下面的问题：\n", FAI.QQ));
                        Console.Write(string.Format("问题:{0} 回答:", FAI.Message));
                        FAI.AnswerMessage = Console.ReadLine();
                    }
                    else if (FAI.ActionProgress == FriendActionProgress.Finished)
                    {

                        Echo(string.Format("添加好友 {0} 完成 :{1}", FAI.QQ, FAI.Message));
                        RemoveItems.Add(FAI.QQ);
                    }
                    
                }
                if (RemoveItems != null && RemoveItems.Count>0)
                { 
                    foreach(int item in RemoveItems)
                    {
                        Client.FriendManager.FriendActionList.Remove(item);
                    }
                }
                Thread.Sleep(100);//休眠
            } while (Client.FriendManager.FriendActionList.UnFinished()!=0);
        }
        //add for interface
        /// <summary>
        /// 输出信息
        /// </summary>
        private static void startmessage()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("================================================================");
            builder.AppendLine("本软件基于QQ2009协议开发，仅用于学习研究，不得用于商业用途");
            builder.AppendLine(" ");
            builder.AppendLine("作者:天行健 QQ:158650331");
            builder.AppendLine(" ");
            builder.AppendLine("如果您破解、销售该产品或同类产品，我们将取消一切技术支持。");
            builder.AppendLine(" ");
            builder.AppendLine("输入英文字母x并回车后退出，输入help查看帮助。");
            builder.AppendLine("================================================================");
            Echo(builder.ToString());
        }

        /// <summary>
        /// web接口类
        /// </summary>
        private static string web(string Lxid, string Qunid, string Qunnid, string Sender, string Nick, string Message)
        {
            HttpWebResponse response;
            string str2;
            string s = ((((("lxid=" + Lxid) + "&qunid=" + Qunid) + "&qunnid=" + Qunnid) + "&Sender=" + Sender) + "&Nick=" + Nick) + "&Message=" + Message;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            if (bianma == "gb2312")
            {
                byte[] bytes = Encoding.Default.GetBytes(s);
                request.ContentLength = bytes.Length;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                    goto Label_0129;
                }
            }
            if (bianma == "utf-8")
            {
                byte[] buffer = Encoding.UTF8.GetBytes(s);
                request.ContentLength = buffer.Length;
                using (Stream stream2 = request.GetRequestStream())
                {
                    stream2.Write(buffer, 0, buffer.Length);
                    stream2.Close();
                    goto Label_0129;
                }
            }
            Echo("机器人网页接口页面编码错误！常用类型utf-8和gb2312！");
        Label_0129:
            response = request.GetResponse() as HttpWebResponse;
            using (Stream stream3 = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream3, Encoding.GetEncoding(bianma)))
                {
                    str2 = reader.ReadToEnd();
                }
            }
            returncode = str2;
            return returncode;
        }
        private static string get()
        {
            string str;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";

            try
            {
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(bianma)))
                    {
                        str = reader.ReadToEnd();
                    }
                }
                getcode = str;
                return getcode;
            }
            catch (Exception)
            {
                //Echo(" 连接接口服务器失败！");
                //return Exception.ReferenceEquals;
                getcode = "-1";
                return getcode;
            }

        }
        private static void getsleep(object source, ElapsedEventArgs e)
        {
            get();
            if (getcode != "")
            {
                string[] strArray = Regex.Split(getcode, "\n");
                string str2 = strArray[0];
                int qq = strArray.Length > 2?int.Parse(strArray[1]):0;
                int index = 2;
                string msg = "";
                if (strArray.Length > 2)
                {
                    while (index < strArray.Length)
                    {
                        msg = msg + strArray[index] + " ";
                        index++;
                    }
                }
                switch (str2)
                {
                    case "-4":
                        {

                            Echo("签名！");
                            return;
                        }
                    case "-1":
                        {

                            Echo("连接接口服务器失败！请检查配置文件！");
                            return;
                        }
                    case "0":
                        {
                            int key = qq;
                            if (Client.QQUser.Friends.ContainsKey(key))
                            {
                                TQQ = Client.QQUser.QQList[key];
                                SendMsg(TQQ, msg);
                                TQQ = null;
                                return;
                            }
                            else
                            {
                                Echo("未能在好友列表中找到该QQ号码！");
                                return;
                            }
                        }
                    case "2":
                        {
                            int num4 = qq;
                            if (Client.QQUser.ClusterList.ContainsKey(num4))
                            {
                                TQQ = Client.QQUser.ClusterList[num4].QQBasicInfo;
                                SendMsg(TQQ, msg);
                                TQQ = null;
                                return;
                            }
                            else
                            {
                                Echo("未找到该群号码！");
                                return;
                            }
                        }
                    case "3":
                        Client.LoginManager.Logout();
                        Console.WriteLine();
                        Console.WriteLine("退出成功");
                        Thread.Sleep(0x7d0);
                        Client.LoginStatus = LoginStatus.Logout;
                        StartLogin();
                        AddEvents();
                        startmessage();
                        return;

                    case "4":
                        Process.Start("update.bat");
                        return;

                    case "5":
                        {
                            int num5 = qq;
                            if (Client.QQUser.Friends.ContainsKey(num5))
                            {
                                Client.MessageManager.SendVibration(num5);
                                Echo(string.Format("你给 {1}[{0}] 发送振动", num5, Client.QQUser.Friends[num5].Nick));
                                return;
                            }
                            else
                            {
                                Echo("未能在好友列表中找到该QQ号码！");
                                return;
                            }
                        }
                    case "on":
                        Program.AutoReply = "start";
                        SendMsg(qq, msg);
                        Echo("接口已开启自动回复功能！");
                        return;

                    case "off":
                        Program.AutoReply = "sleep";
                        SendMsg(qq, msg);
                        Echo("接口已关闭自动回复功能！");
                        return;

                    case "qunon":
                        Program.qunAutoReply = "start";
                        Client.ClusterManager.SendClusterIM(qq, msg);
                        Echo("接口开启闭群自动回复功能！");
                        return;

                    case "qunoff":
                        Program.qunAutoReply = "sleep";
                        Client.ClusterManager.SendClusterIM(qq, msg);
                        Echo("接口已关闭群自动回复功能！");
                        break;
                }
            }
        }
        private static void MessageManager_ReceiveNormalIM(object sender, TS.QQ.Events.QQEventArgs<ReceiveIMPacket, OutPacket> e)
        {
            if (back == "T")
            {
                Echo(string.Format("{1}[{2}] 说:{3}", new object[] { Util.GetDateTimeFromMillis(e.InPacket.NormalHeader.SendTime), e.InPacket.Header.Sender, e.QQClient.QQUser.Friends[e.InPacket.Header.Sender].Nick, e.InPacket.NormalIM.Message }));
            }
            string lxid = "0";
            string qunid = "0";
            string qunnid = "0";
            string nick = e.QQClient.QQUser.Friends[e.InPacket.Header.Sender].Nick;
            string str5 = e.InPacket.Header.Sender.ToString();
            string message = e.InPacket.NormalIM.Message;
            if (AutoReply == "start")
            {
                web(lxid, qunid, qunnid, str5, nick, message);
                if (returncode != "")
                {
                    SendMsg(e.InPacket.NormalHeader.Sender, returncode);
                }
            }
            else if (str5 == adminqq)
            {
                web(lxid, qunid, qunnid, str5, nick, message);
                if (returncode != "")
                {
                    SendMsg(e.InPacket.NormalHeader.Sender, returncode);
                }
            }
            else
            {
                Echo("自动回复已关闭！您的消息无法回答！");
            }
        }
    }
}
